package com.redteam.data.dao;

import com.redteam.data.beans.Simulator;
import com.redteam.data.dao.interfaces.DAO;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

/**
 * A DAO for <code>Simulators</code>
 * @author Jezreel
 */
public class SimulatorDAO extends AbstractDAO implements DAO {

    private static SimulatorDAO dao;
    private static DataSource ds;
    private final String tableName;

    static {
        dao = new SimulatorDAO();
    }

    private SimulatorDAO() {
        getDataSource();
        tableName = "HIGHFLIERS.RentalItems";
    }

    /**
     * Static method for getting <code>SimulatorDAO</code>
     * @return
     */
    public static SimulatorDAO getInstance() {
        return dao;
    }

    private void getDataSource() {
        try {
            InitialContext ic = new InitialContext();
            ds = (DataSource) ic.lookup("java:comp/env/jdbc/redteam");

        } catch (NamingException ex) {
            Logger.getLogger(SimulatorDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * (@inherentDoc)
     */
    @Override
    public String buildUpdateQuery(Object obj) {
        Simulator simulator = (Simulator) obj;
        StringBuilder sb = new StringBuilder("UPDATE ");
        sb.append(getTableName()).append(" SET RATE = ").append(simulator.getRate());
        sb.append(" WHERE ID = ").append(simulator.getId());
        return sb.toString();
    }

    /**
     * (@inherentDoc)
     */
    @Override
    public Simulator getObjectOut(ResultSet rs) {
        Simulator simulator = new Simulator();
        try {
            simulator.setId(rs.getLong("ID"));
            simulator.setFboID(rs.getLong("FBO_ID"));
            simulator.setType(rs.getString("DISCRIMINATOR"));
            simulator.setRate(rs.getDouble("RATE"));
            simulator.setAircraftType(rs.getString("TYPE"));
        } catch (SQLException ex) {
            Logger.getLogger(SimulatorDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return simulator;
    }

    /**
     * (@inherentDoc)
     */
    @Override
    public String buildInsertQuery(Object obj) {
        Simulator simulator = (Simulator) obj;
        StringBuilder sb = new StringBuilder("INSERT INTO ").append(this.getTableName());
        sb.append(" (FBO_ID,RATE,TYPE,DISCRIMINATOR");
        sb.append(") VALUES (").append(simulator.getFboID()).append(",").append(simulator.getRate());
        sb.append(",'").append(simulator.getAircraftType()).append("','").append(simulator.getType()).append("')");
        return sb.toString();
    }

    /**
     * (@inherentDoc)
     */
    @Override
    public long getObjectId(Object o) {
        Simulator simulator = (Simulator) o;
        return simulator.getId();
    }

    /**
     * (@inherentDoc)
     */
    @Override
    public DataSource getDS() {
        return ds;
    }

    /**
     * (@inherentDoc)
     */
    @Override
    public String getTableName() {
        return tableName;

    }

    /**
     * (@inherentDoc)
     */
    @Override
    public List<Object> getAll() throws SQLException {
        return getAll("simulator");
    }

    /**
     * (@inherentDoc)
     */
    @Override
    public Object getSingle(long id) throws SQLException {
        return getSingle(id, "simulator");
    }
}
